package sketch.shape.domain;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import sketch.doc.Sketch;
import sketch.shape.Shape;
import sketch.shape.ShapeOption;
import sketch.shape.geom.prim.Line;

public class Link extends Shape {

	private List<LinkEnd> ends;

	public void drawComponent(Graphics2D g2) {
		super.drawComponent(g2);

		for (LinkEnd le : getLinkEnds()) {
			le.drawComponent(g2);
		}

//		int x[] = new int[getLinkEnds().size()];
//		int y[] = new int[getLinkEnds().size()];
//
//		for (int i = 0; i < getLinkEnds().size(); i++) {
//			Point p = getLinkEnds().get(i).getEndPoint();
//			x[i] = p.intX();
//			y[i] = p.intY();
//		}


	}

	public List<LinkEnd> getLinkEnds() {
		return ends;
	}

	public void setLinkEnds(List<LinkEnd> newEnds) {
		ends = newEnds;
	}

	public List<Line> getPath() {
		List<Line> path = new ArrayList<Line>();
		for (LinkEnd le : getLinkEnds()) {
			path.addAll(le.getPath());
		}
		return path;
	}

	public void drawSelected(Graphics2D g2) {
		drawWith(g2, ShapeOption.selectionColor, ShapeOption.selectionWidth);
	}

	public java.awt.Shape getBound(double GAP) {
		Sketch sketch = getHost();
		if (sketch == null)
			return super.getBound(GAP);

		java.awt.Shape shape = null;
		for (LinkEnd e : getLinkEnds()) {
			java.awt.Shape sh = e.getBound();
			if (shape == null)
				shape = sh;
			else
				shape = shape.getBounds().union(sh.getBounds());
		}
		if (shape == null)
			return super.getBound(GAP);

		return shape;
	}

	public String[][] getFieldValues() {
		return new String[][] {
				{ "Number of Ends", Integer.toString(getLinkEnds().size()) }
		};
	}

	public Link() {
		super();
		ends = new ArrayList<LinkEnd>();
	}

	public void appendLinkEnd(LinkEnd le) {
		ends.add(le);
	}

}
